package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.Student;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.ConsumptionQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.utils.UserContext;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Transactional
public class ConsumptionServiceImpl implements IConsumptionService {
    @Autowired
    private ConsumptionMapper consumptionMapper ;

    @Autowired
    private IAppointmentService appointmentService;
    @Override
    public void insert(Consumption consumption) {
        consumptionMapper.insert(consumption);
    }

    @Override
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Consumption consumption) {
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public Consumption selectById(Long id) {
        Consumption consumption = consumptionMapper.selectByPrimaryKey(id);
        return consumption ;
    }

    @Override
    public List<Consumption> selectAll() {
        List<Consumption> consumptions = consumptionMapper.selectAll();
        return consumptions ;
    }

    @Override
    public PageInfo<Consumption> query(ConsumptionQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Consumption> consumptions = consumptionMapper.queryAll(qo);
        return new PageInfo<>(consumptions);
    }

    @Override
    public void save(Consumption consumption) {

        //创建用户与时间
        consumption.setCreateUserId(UserContext.getEmployee().getId());
        consumption.setCreateTime(new Date());
        consumption.setStatus(Consumption.STATUS_TYPE_CONSUME);
        //流水号
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        int random = new Random().nextInt(90000)+10000;
        consumption.setCno(date+random);

        consumptionMapper.insert(consumption);

    }

    @Override
    public Long saveOrUpdate(Long aid) {
        //预约单状态:消费中状态
        appointmentService.updateStatus(aid, Appointment.STATUS_TYPE_CONSUME);
        //新增预约的关联结算单
        Consumption consumption = new Consumption();
        Appointment appointment = appointmentService.get(aid);
        //创建用户与时间
        consumption.setCreateUserId(UserContext.getEmployee().getId());
        consumption.setCreateTime(new Date());
        consumption.setStatus(Consumption.STATUS_TYPE_CONSUME);
        //1:流水号
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        int random = new Random().nextInt(90000) + 10000;
        consumption.setCno(date + random);
        //关联的预约单流水号
        consumption.setAppointmentAno(appointment.getAno());
        //业务类型
        consumption.setBusiness(appointment.getBusiness());
        //客户名称与电话
        consumption.setCustomerName(appointment.getContactName());
        consumption.setCustomerTel(appointment.getContactTel());
        //备注
        consumption.setInfo(appointment.getInfo());

        consumptionMapper.insert(consumption);

        return consumption.getId();
    }

}
